package edu.cs.usask.ca.silver.plugin.rcp.editors;

import java.util.ArrayList;

import org.apache.log4j.Logger;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.util.IPropertyChangeListener;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IPersistableElement;
import org.eclipse.ui.plugin.AbstractUIPlugin;

import edu.cs.usask.ca.silver.businesslogic.ids.BaseId;
import edu.cs.usask.ca.silver.plugin.rcp.app.Application;
import edu.cs.usask.ca.silver.plugin.rcp.components.SilverEntitiesIncludingIconsEnumerations;
import edu.cs.usask.ca.silver.plugin.rcp.components.SilverTreeObject;
import edu.cs.usask.ca.silver.plugin.rcp.jobs.LoggerReportScheduler;
import edu.cs.usask.ca.silver.plugin.rcp.views.projectviewer.ProjectViewI;

/**
 * 
 * This class is used to store the base id associated with the selected element
 * form the tree viewer. It also stores the name of the of the selected
 * element.This information is used by the info editor to create the displays
 * needed.
 * 
 * @author Mike Taylor
 */
public class InfoEditorInput implements IEditorInput {

	private final BaseId modelObjectID;
	private String modelObjectName;
	private String modelObjectType;
	public ProjectViewI projectView;
	private final ArrayList<IPropertyChangeListener> listeners;
	private static Logger logger = Logger.getLogger(InfoEditorInput.class);

	/**
	 * Constructor for the infoeditorinput associated with a input editor
	 * 
	 * @param treeObject
	 *            selected element from the tree viewer
	 * @param projectView
	 *            the ProjectViewer that all data is displayed in
	 */
	public InfoEditorInput(SilverTreeObject treeObject,
			ProjectViewI projectView) {
		super();
		assert (treeObject != null);
		/* this could be a call to the DB for this info given the new ID objects */
		modelObjectID = treeObject.getSilverObjectId();
		try {
			modelObjectName = treeObject.getSilverObjectId()
					.getEntryNameFromDatabase(projectView.getAssociatedStorage());
		} catch (NullPointerException e1) {
			LoggerReportScheduler.scheduleErrorLogging(logger,"Nullpointer exception while getting silver id object name", e1);
		} catch (Exception e) {
			LoggerReportScheduler.scheduleErrorLogging(logger,"Exception while getting silver id object name", e);
		}
		logger.info("This is the InfoInput name for current object: "
				+ modelObjectName);

		this.projectView = projectView;
		listeners = new ArrayList<IPropertyChangeListener>();

	}

	public boolean exists() {
		return false;
	}

	/**
	 * This is a method used to get the appropriate icon image to display in the
	 * tab of the individual editors.
	 * 
	 * @return the appropriate ImageDescriptor for model object selected
	 */
	public ImageDescriptor getImageDescriptor() {
		try {
			return (AbstractUIPlugin
					.imageDescriptorFromPlugin(
							Application.ID,
							SilverEntitiesIncludingIconsEnumerations
									.getEnumIdIconPathBasedOnBaseIdClassName(modelObjectID)));
		} catch (NullPointerException e1) {
			LoggerReportScheduler.scheduleErrorLogging(logger,"Nullpointer exception while getting icon by base id type", e1);
		} catch (Exception e) {
			LoggerReportScheduler.scheduleErrorLogging(logger,"Exception while getting icon by base id name", e);
		}
		return null;
	}

	public String getName() {
		return modelObjectName;
	}

	public void setName(String newObjectName) {
		modelObjectName = newObjectName;
	}

	/**
	 * gets the name of the model object being displayed in this editor return
	 * the name of the object displayed in this editor NOT SURE IF I NEED THIS
	 * RIGHT NOW
	 */
	public String getModelObjectName() {
		return modelObjectName;

	}

	/**
	 * gets the ID of the model object being displayed in this editor
	 * 
	 * @return the ID of the object displayed in this editor
	 */
	public BaseId getModelObjectID() {
		return modelObjectID;

	}

	public IPersistableElement getPersistable() {
		return null;
	}

	/**
	 * get the tool tip for tabs, this can not be null
	 */
	public String getToolTipText() {
		return modelObjectName;
	}

	public Object getAdapter(Class adapter) {
		return null;
	}

	public ProjectViewI getProjectView() {
		return projectView;
	}

	/**
	 * A method that keeps the user from opening multiple windows for the same
	 * modelObject
	 * 
	 * @param the
	 *            selected object from the structured tree viewer
	 * @return true if the selected element for the structured tree viewer is
	 *         already open false otherwise
	 */
	@Override
	public boolean equals(Object obj) {
		if (super.equals(obj))
			return true;
		if (!(obj instanceof InfoEditorInput))
			return false;
		InfoEditorInput other = (InfoEditorInput) obj;
		return this.modelObjectID.equals(other.modelObjectID);
	}/* end of equals */

	// A public method that allows listener registration
	public void addPropertyChangeListener(IPropertyChangeListener listener) {

		if (!listeners.contains(listener))
			listeners.add(listener);

	}

	// A public method that allows listener registration
	public void removePropertyChangeListener(IPropertyChangeListener listener) {
		listeners.remove(listener);

	}

}/* end of class */
